package com.tozed.test;

import com.tozed.widget.TitleBar;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class UsbTestActivity extends Base implements OnClickListener
{
	private static final String TAG = "UsbTestActivity";
	
	
	private TitleBar mTitleBar;
	private TextView mTextInfo;
	private Button mBtnPass, mBtnFail;
	
	private EditText mTextUsb1;
	private EditText mTextUsb2;
	
	private int mTestType = Constant.TYPE_TEST_SINGLE;
	private boolean mIsUSB1Pass = false;
	private boolean mIsUSB2Pass = false;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_usb);
		
		Intent intent = getIntent();
		if (intent != null)
			mTestType = intent.getIntExtra("TEST_TYPE", Constant.TYPE_TEST_SINGLE);
		
		setupViews();
	}
	
	private void setupViews(){
		mTitleBar = (TitleBar)findViewById(R.id.title_bar);
		mTitleBar.setTiteText(R.string.str_test_usb);
		mTitleBar.hideLeftButton();
		mTitleBar.hideRightButton();
		
		mBtnPass = (Button)findViewById(R.id.btn_pass);
		mBtnFail = (Button)findViewById(R.id.btn_fail);
		mBtnPass.setOnClickListener(this);
		mBtnFail.setOnClickListener(this);
		mBtnPass.setEnabled(false);
		
		mTextInfo = (TextView)findViewById(R.id.text_tip);
		mTextUsb1 = (EditText)findViewById(R.id.text_usb1);
		mTextUsb2 = (EditText)findViewById(R.id.text_usb2);
		
		mTextUsb1.addTextChangedListener(mWatcherUsb1);
		mTextUsb2.addTextChangedListener(mWatcherUsb2);
		
		mTextInfo.setText(getString(R.string.str_usb_insert_usb1_tip));
	}
	
	private TextWatcher mWatcherUsb1 = new TextWatcher()
	{
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count){
			String str = mTextUsb1.getText().toString().trim();
			if (str == null || str.length() <= 0)
				return;
			
			mTextInfo.setText(getString(R.string.str_usb_insert_usb2_tip));
			
			mIsUSB1Pass = true;
			if (mIsUSB1Pass && mIsUSB2Pass){
				mBtnPass.setEnabled(true);					
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after){
		}

		@Override
		public void afterTextChanged(Editable s){
		}
	};
	
	private TextWatcher mWatcherUsb2 = new TextWatcher()
	{
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count){
			String str = mTextUsb2.getText().toString().trim();
			if (str == null || str.length() <= 0)
				return;
			
			mTextInfo.setText(getString(R.string.str_usb_detect_data));

			mIsUSB2Pass = true;
			if (mIsUSB1Pass && mIsUSB2Pass){
				mBtnPass.setEnabled(true);					
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after){
		}

		@Override
		public void afterTextChanged(Editable s){
		}
	};
	
	@Override
	public void onBackPressed() {
	    if (mTestType == Constant.TYPE_TEST_AGING) {
			Intent intent = new Intent();
			intent.putExtra("KEY_TEST_STATUS", 0);
			setResult(RESULT_OK, intent);
	    }
	    
	    super.onBackPressed();
	}
	 
	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.btn_pass:
			Intent intent = new Intent();
			intent.putExtra("KEY_TEST_STATUS", 1);
			setResult(RESULT_OK, intent);
			finish();
			break;
			
		case R.id.btn_fail:
			Intent intent2 = new Intent();
			intent2.putExtra("KEY_TEST_STATUS", 0);
			setResult(RESULT_OK, intent2);
			finish();
			break;
			
		default:
			break;
		}
	}
}
